//
//  ExercisesPageSubItemView.m
//  ShowYork
//
//  Created by hxh on 2020/9/8.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "ExercisesPageSubItemView.h"

@interface ExercisesPageSubItemView ()
@property (nonatomic,strong)UILabel *titleLb;
@property (nonatomic,strong)UILabel *scoreLb;
@property (nonatomic,strong)UIImageView *line;
@end

@implementation ExercisesPageSubItemView

- (instancetype)initWithItemTitle:(NSString *)title andScore:(NSString *)score{
    if (self = [super init]) {
        [self setUI];
        self.titleLb.text = title;
        self.scoreLb.text = [NSString stringWithFormat:@"共%@分",score];
    }
    return self;
}
- (void)setUI{
    self.titleLb = [[UILabel alloc]init];
    self.titleLb.font = font(14);
    self.titleLb.textColor = RGBA(51, 51, 51, 1);
    [self addSubview:self.titleLb];
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(26);
        make.top.bottom.equalTo(self);
        make.height.mas_equalTo(20).priorityHigh();
    }];
    
    self.scoreLb = [[UILabel alloc]init];
    self.scoreLb.font = font(14);
    self.scoreLb.textColor = RGBA(51, 51, 51, 1);
    [self addSubview:self.scoreLb];
    [self.scoreLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLb);
        make.right.equalTo(self).offset(-26);
        make.height.mas_equalTo(20);
    }];
    
    self.line = [[UIImageView alloc]init];
    
    [self addSubview:self.line];
    
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLb);
        make.left.equalTo(self.titleLb.mas_right).offset(7);
        make.right.equalTo(self.scoreLb.mas_left).offset(-7);
        make.height.mas_equalTo(2);
    }];
 
    
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.line.image = [self drawLineOfDashByImageView:self.line];
}

- (UIImage *)drawLineOfDashByImageView:(UIImageView *)imageView {
    // 开始划线 划线的frame
    UIGraphicsBeginImageContext(imageView.frame.size);

    [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];

    // 获取上下文
    CGContextRef line = UIGraphicsGetCurrentContext();

    // 设置线条终点的形状
    CGContextSetLineCap(line, kCGLineCapRound);
    // 设置虚线的长度 和 间距
    CGFloat lengths[] = {5,5};

    CGContextSetStrokeColorWithColor(line, RGBA(221, 221, 221, 1).CGColor);
    // 开始绘制虚线
    CGContextSetLineDash(line, 0, lengths, 2);

    CGContextMoveToPoint(line, 0.0, 2.0);

    CGContextAddLineToPoint(line, 300, 2.0);

    CGContextStrokePath(line);

    // UIGraphicsGetImageFromCurrentImageContext()返回的就是image
    return UIGraphicsGetImageFromCurrentImageContext();
}



@end
